package com.css.fxfzaqbz.modules.estimate.repository.impl;

import com.css.fxfzaqbz.modules.estimate.entity.Fault;
import com.css.fxfzaqbz.modules.estimate.entity.InstrumentCatalog;
import com.css.fxfzaqbz.modules.estimate.repository.InstrumentCatalogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class InstrumentCatalogRepositoryImpl implements InstrumentCatalogRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void batchSave(List<InstrumentCatalog> list2) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list2.toArray());
        String sql = "INSERT INTO sys_instrument_catalog(jic_id, province, city, county, town, earthquake_code, event_abbreviation, occurrencedate, occurrencetime, location, jic_longitude, jic_latitude, magnitude, depth, epicenter, isoline, epicentral_intensity, accuracy, lastangle, comment_info, source_flag, del_flag, data_version, create_user, create_time, update_user, update_time, geom, ilgraph_aiid, ilgraph_arwid, extends1, extends2, extends3, extends4, extends5, extends6, extends7, extends8, extends9, extends10, result_analysis) values(:jicId, :province, :city, :county, :town, :earthquakeCode, :eventAbbreviation, :occurrencedate, :occurrencetime, :location, :jicLongitude, :jicLatitude, :magnitude, :depth, :epicenter, :isoline, :epicentralIntensity, :accuracy, :lastangle, :commentInfo, :sourceFlag, :delFlag, :dataVersion, :createUser, :createTime, :updateUser, :updateTime, ST_GeometryFromText(:geom, 4490), :ilgraphAiid, :ilgraphArwid, :extends1, :extends2, :extends3, :extends4, :extends5, :extends6, :extends7, :extends8, :extends9, :extends10, :resultAnalysis)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<InstrumentCatalog> getList(String resultAnalysis) {
        String sql = "select *,ST_AsText(geom) geom from sys_instrument_catalog where result_analysis = '" + resultAnalysis + "'";
        List<InstrumentCatalog> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(InstrumentCatalog.class));
        return list;

    }
}
